package org.lb.MarketingSystem.controller;

import java.util.ArrayList;
import java.util.List;

import org.lb.MarketingSystem.entity.CompanyUser;
import org.lb.MarketingSystem.service.CompanyUserService;
import org.lb.MarketingSystem.service.CustomerUserService;
import org.lb.MarketingSystem.service.DepartmentService;
import org.lb.MarketingSystem.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/system")
public class MarketingSystemController {

	@Autowired
	CompanyUserService companyUserServiceImpl;
	@Autowired
	DepartmentService departmentServiceImpl;
	@Autowired
	PositionService positionServiceImpl;
	@Autowired
	CustomerUserService customerUserServiceImpl;

	@RequestMapping(value = "/staff_login", method = RequestMethod.GET)
	public String staff_login(Model model) {
		return "staff_login";
	}

	@RequestMapping(value = "/staff_login2", method = RequestMethod.POST)
	public String JumpJsp(String account_number, String password, Model model) {
		CompanyUser companyUser = companyUserServiceImpl.getCompanyUser(account_number, password);
		if (companyUser != null) {
			model.addAttribute("user", companyUser);
			model.addAttribute("position", positionServiceImpl.get_allPosition());
			model.addAttribute("department", departmentServiceImpl.get_allDepartment());
			return "manage";
		}
		return "staff_login";
	}

	@RequestMapping(value = "/manage", method = RequestMethod.POST)
	public String managepage(String Per_acc, String Per_pass, String Per_sex, String Per_idnum, Integer Per_age,
			String Per_birth, String Per_addre, String Per_pho, Model model) {
		CompanyUser companyUser = companyUserServiceImpl.getCompanyUserByAccount(Per_acc);
		if (Per_pass != null && !Per_pass.isEmpty() && !Per_pass.equals("")) {
			companyUser.setPassword(Per_pass);
		}
		if (Per_age != null && !Per_age.equals("")) {
			companyUser.setAge(Per_age);
		}
		if (Per_addre != null && !Per_addre.isEmpty() && !Per_addre.equals("")) {
			companyUser.setAddress(Per_addre);
		}
		if (Per_birth != null && !Per_birth.isEmpty() && !Per_birth.equals("")) {
			companyUser.setBirthdate(Per_birth);
		}
		if (Per_pho != null && !Per_pho.isEmpty() && !Per_pho.equals("")) {
			companyUser.setPhonenumber(Per_pho);
		}
		if (Per_idnum != null && !Per_idnum.isEmpty() && !Per_idnum.equals("")) {
			companyUser.setIdNumber(Per_idnum);
		}
		if (Per_sex != null && !Per_sex.isEmpty() && !Per_sex.equals("")) {
			companyUser.setSex(Per_sex);
		}
		companyUserServiceImpl.updateCompanyUser(companyUser);
		companyUser = companyUserServiceImpl.getCompanyUserByAccount(Per_acc);
		model.addAttribute("user", companyUser);
		model.addAttribute("position", positionServiceImpl.get_allPosition());
		model.addAttribute("department", departmentServiceImpl.get_allDepartment());
		return "manage";
	}

	@RequestMapping(value = "/get_companyusers")
	@ResponseBody
	public List<Object> getcompanyusers(Integer pagenum,Integer type,String data) {
		List<Object> list = new ArrayList<Object>();
		Integer counts=0;
		if(type==1)
		{
		counts=companyUserServiceImpl.getpagenumbers(companyUserServiceImpl.getCounts(), 12);
		list.add(counts);
		list.add(companyUserServiceImpl.getCompanyuserByPage(pagenum*12, 12));
		}
		if(type==2)
		{
			String data1[]=data.split(",");
			List<CompanyUser> companyUsers=selectcompanyusers(data1[1], data1[0],data1[2],data1[3]);
			List<CompanyUser> cList=new ArrayList<CompanyUser>();
			for(int i=0+pagenum*12;i<=(pagenum+1)*12-1;i++)
			{
				if(i>=companyUsers.size())
				{
					break;
				}else{	
				  cList.add(companyUsers.get(i));
				}
			}
			counts=companyUserServiceImpl.getpagenumbers(companyUsers.size(),12);
			list.add(counts);
			list.add(cList);
		}
		if(type==3)
		{
			deltetCompanyusers(data);
			list.add("delete");
		}
		return list;
	}

	public List<CompanyUser> selectcompanyusers(String account, String name, String department, String position) {
		List<CompanyUser> list = new ArrayList<CompanyUser>();
		list = companyUserServiceImpl.get_allCompanyUser();
		List<CompanyUser> list1 = new ArrayList<CompanyUser>();
		if (!account.equals("") && account != null && !account.isEmpty()) {
			CompanyUser companyUser = companyUserServiceImpl.getCompanyUserByAccount(account);
			if (companyUser == null) {
			} else {
				if (!name.equals("") && name != null && !name.isEmpty()) {
					if (companyUser.getName().equals(name)) {
						if (department.equals("department0") && position.equals("position0")) {
							list1.add(companyUser);
						} else {
							if (department.equals("department0")) {
								if (companyUser.getPosition().equals(position)) {
									list1.add(companyUser);
								}
							} else {
								if (companyUser.getDepartment().equals(department)) {
									if (position.equals("position0")) {
										list1.add(companyUser);
									} else {
										if (companyUser.getPosition().equals(position)) {
											list1.add(companyUser);
										} 
									}
								}
							}
						}
					} else {
					}
				} else {
					if (department.equals("department0") && position.equals("position0")) {
						list1.add(companyUser);
					} else {
						if (department.equals("department0")) {
							if (companyUser.getPosition().equals(position)) {
								list1.add(companyUser);
							}
						} else {
							if (companyUser.getDepartment().equals(department)) {
								if (position.equals("position0")) {
									list1.add(companyUser);
								} else {
									if (companyUser.getPosition().equals(position)) {
										list1.add(companyUser);
									}
								}
							}
						}
					}
				}
			}
		} else {
			if (!name.equals("") && name != null && !name.isEmpty()) {
				List<CompanyUser> list2 = companyUserServiceImpl.getCompanyUserByName(name);
				if (list2!=null) {
					if (department.equals("department0") && position.equals("position0")) {
						list1 = list2;
					} else {
						if (department.equals("department0")) {
							for (CompanyUser companyUser : list2) {
								if (companyUser.getPosition().equals(position)) {
									list1.add(companyUser);
								}
							}
						} else {
							List<CompanyUser> list3 = new ArrayList<CompanyUser>();
							for (CompanyUser companyUser : list2) {
								if (companyUser.getDepartment().equals(department)) {
									list3.add(companyUser);
								}
							}
							if (list3.size() > 0) {
								if (position.equals("position0")) {
									list1 = list3;
								} else {
									for (CompanyUser companyUser : list3) {
										if (companyUser.getPosition().equals(position)) {
											list1.add(companyUser);
										}
									}
								}
							}
						}
					}
				}
			} else {
				if (department.equals("department0") && position.equals("position0")) {
					list1 = list;
				} else {
					if (department.equals("department0")) {
						for (CompanyUser companyUser : list) {
							if (companyUser.getPosition().equals(position)) {
								list1.add(companyUser);
							}
						}
					} else {
						List<CompanyUser> list3 = new ArrayList<CompanyUser>();
						for (CompanyUser companyUser : list) {
							if (companyUser.getDepartment().equals(department)) {
								list3.add(companyUser);
							}
						}
						if (list3.size() > 0) {
							if (position.equals("position0")) {
								list1 = list3;
							} else {
								for (CompanyUser companyUser : list3) {
									if (companyUser.getPosition().equals(position)) {
										list1.add(companyUser);
									}
								}
							}
						}
					}
				}
			}
		}
		return list1;
	}
	
	@RequestMapping(value = "/add_companyuser")
	@ResponseBody
	public List<CompanyUser> addCompanyuser(String account, String name, String department, String position,Double salary)
	{
		List<CompanyUser> addUser=new ArrayList<CompanyUser>();
		CompanyUser companyUser=new CompanyUser();
		companyUser.setAccountNumber(account);
		companyUser.setName(name);
		companyUser.setDepartment(department);
		companyUser.setPosition(position);
		companyUser.setSalary(salary);
		companyUser.setPassword("123");
		addUser.add(companyUserServiceImpl.addCompanyUser(companyUser));
		return addUser;		
	}
	

	public void deltetCompanyusers(String ids)
	{
	    String ID[]=ids.split(",");
	    for(String string:ID)
	    {
	    	if(!string.equals(""))
	    	{
	    		Integer id=Integer.valueOf(string);
	    		companyUserServiceImpl.deleteCompanyUser(id);
	    	}
	    }
	}
	@RequestMapping(value = "/update_companyuser")
	@ResponseBody
	public List<CompanyUser> getcompanyusers(String data){
		String data1[]=data.split(",");
		CompanyUser companyUser = companyUserServiceImpl.getCompanyUserByAccount(data1[0]);
		if (data1[0] != null && !data1[0].isEmpty() && !data1[0].equals(""))
		{
			companyUser.setAccountNumber(data1[0]);				
		}
		if (data1[1] != null && !data1[1].isEmpty() && !data1[1].equals(""))
		{
			companyUser.setPassword(data1[1]);
		}
		if (data1[2] != null && !data1[2].isEmpty() && !data1[2].equals(""))
		{
			companyUser.setName(data1[2]);
		}
		if (data1[3] != null && !data1[3].isEmpty() && !data1[3].equals(""))
		{
			companyUser.setSalary(Double.valueOf(data1[3]));
		}
		if (!data1[4].equals("position0"))
		{
			companyUser.setPosition(data1[4]);
		}
		if (!data1[5].equals("department0"))
		{
			companyUser.setDepartment(data1[5]);
		}
		companyUserServiceImpl.updateCompanyUser(companyUser);
		List<CompanyUser> cUsers=new ArrayList<CompanyUser>();
		cUsers.add(companyUserServiceImpl.getCompanyUserByAccount(data1[0]));
		return cUsers;		
	}
	
	@RequestMapping(value = "/getCusInfo")
	@ResponseBody
	public List<Object> getCusInfo(Integer pagenum,String data,Integer type)
	{
		List<Object> list=new ArrayList<Object>();
		Integer counts=0;
		if(type==0)
		{
			System.out.println(customerUserServiceImpl.getCounts());
			counts=companyUserServiceImpl.getpagenumbers(customerUserServiceImpl.getCounts(), 12);			
			list.add(counts);
			list.add(customerUserServiceImpl.getCustomeruserByPage(pagenum*12, 12));
		}
		return list;		
	}
}
